/******************* (C) COPYRIGHT 2022 CHU WEI TENG ***********************************************
**
**------------------ File Info ---------------------------------------------------------------------
** File name:      dwin_edit.c
** modified Date:  2022-05-10
** Last Version:   V1.0
** Descriptions: 
** 
**------------------ Modfication  log --------------------------------------------------------------
** Date       Who            Rev        Comments
----------------------------------------------------------------------------------------------------
   22/05/10  chuwt@qq.com    V1.00      created

****************************************************************************************************/
#include <string.h>
#include "dwin_edit.h"
#include "dwin_trans.h"

/* Private define----------------------------------------------------------------------------------*/

/* Private typedef---------------------------------------------------------------------------------*/

/* Private variables-------------------------------------------------------------------------------*/
static ret_t edit_event_dispatch(dwin_widget_t* edit, void* ctx);

dwin_widget_vtable_t edit_vt = 
{
    .obj_size =  sizeof(dwin_edit_t),
    .dispatch = edit_event_dispatch,
};

/* public function--------------------------------------------------------------------------------*/

dwin_widget_t* dwin_edit_create(dwin_widget_t* parent, uint16_t addr, uint16_t attr, uint16_t size)
{
    dwin_widget_t* edit = dwin_widget_create(parent, &edit_vt, size);

    if (edit != NULL)
    {
        edit->active = 1;
        edit->addr = addr;
        edit->attr = attr;       
        edit->size = size;             
        edit->widget_type = DWIN_EDIT;
    }
    return edit;
}

ret_t dwin_edit_write(dwin_widget_t* edit)
{
    return dwin_widget_write(edit);
}

ret_t dwin_multi_edit_write(dwin_widget_t** edit_arry, uint8_t count)
{
    return dwin_multi_widget_write(edit_arry, count);
}

/* Private function-------------------------------------------------------------------------------*/
static ret_t edit_event_dispatch(dwin_widget_t* edit, void* ctx)
{
    dwin_edit_t* obj = (dwin_edit_t *)edit;
    data_pack_t* pack = (data_pack_t *)ctx;
    ret_t        result = RET_FAIL;
	
	obj->reg_id = (pack->addr_high << 8) | pack->addr_low;

    switch(edit->size)
    {
        case 2:
        {   
            memcpy(&obj->word, &pack->data[1], edit->size);
            obj->word = DWIN_SWAPS(obj->word);
            obj->type = EDIT_WORD;
        }break;

        case 4:
        {
            memcpy(&obj->dword, &pack->data[1], edit->size);
            obj->dword = DWIN_SWAPL(obj->dword);
            obj->type = EDIT_DWORD;
        }break;

        case 8:
        {
            memcpy(&obj->qword, &pack->data[1], edit->size);
            obj->qword = DWIN_SWAPLL(obj->qword);
            obj->type = EDIT_QWORD;
        }break;
        
        default : break;
    }

    if (edit->func != NULL)
    {
        edit->func(edit);
    }

    return result;
}

